package com.zsl.service;

import com.zsl.mapper.ProductMapper;
import com.zsl.model.Product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service

@Cacheable(cacheNames = "product")
public class ProductService {

    @Autowired
    private ProductMapper productMapper;


    /**
     *  redis
     *    key - entity
     *    数据类型
     *    在springboot 如何操作redis的数据类型
     * */

    @Cacheable(value = "product", key = "#productId")
    public Product getSingle(Long productId) {

        Optional<Product> optional = productMapper.findById(productId);

        return optional.isPresent() ? optional.get() : null;
    }


    @CacheEvict(value = "product", key = "#productId")
    public Boolean remove(Long productId) {

        productMapper.deleteById(productId);


        return true;
    }


    /**
     *  使用RedisTemplate 提高代码的灵活性
     * **/
    @CachePut(value = "product", key = "#product.productId")
    public Product edit(Product product) {



        return productMapper.save(product);
    }

}
